define(function(require, exports, module) {

	"use strict";
	require("jquery");
	require("mui");
	require("../newOrder/dateInput.js");
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');
	dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏

	var getFunList = require("../getFunList/getFunList.js"); //函数配置js

	if(sessionStorage.goodsInfo) {
		getFunList.infoFromOrderShow(); //编辑新增表单页面点击货品信息进入编辑
	}

	$(function() {

		getFunList.getGoodsWhInvneotory(); //获取货品各仓库库存量

		if(sessionStorage.goodsInfoList) { //定义货品信息列表本地变量
			var goodsInfoList = eval(sessionStorage.goodsInfoList);
			for(var i = 0; i < goodsInfoList.length; i++) {
				goodsInfoList[i] = JSON.parse(goodsInfoList[i]);
			}
		} else {
			var goodsInfoList = [];
		}
		console.log(goodsInfoList);

		mui("body").on("tap", "#finish-btn", function() {
			//			sessionStorage.removeItem("flag");
			sessionStorage.removeItem("goodsInfo");
			subProductDetail();
		});

		function alertTshow(i) {
			$(".alertT").eq(i).stop().animate({
				"top": "70px"
			}, 400, function() {
				$(this).delay(1000).fadeOut(500, function() {
					$(this).css("top", "-35px");
					$(this).show();
				});
			});
		}

		function ADDinvalid(i) {
			$(".testI").eq(i).addClass("invalid");
			$(".testI").eq(i).attr("placeholder", "不能为空");
		}

		function REMOVEinvalid(i) {
			$(".testI").eq(i).removeClass("invalid");
		}

		function subProductDetail() { //提交数据函数
			var goodsBuyNum = $("#goodsNum").val(); //购买数量
			var goodsPrice = $("#goodsPrice").val(); //产品单价
			var goodsWhid = $("#whid").data("id"); //仓库ID
			var goodsWhName = $("#whid").text(); //仓库名字

			if(!goodsWhid) {
				$("#whid").addClass("red");
			} else {
				$("#whid").removeClass("red");
			}
			if(!goodsBuyNum) {
				ADDinvalid(0);
			} else {
				REMOVEinvalid(0);
			}
			//			if(!goodsPrice){
			//				ADDinvalid(1);
			//			}else{
			//				REMOVEinvalid(0)
			//			}

			if(!goodsWhid) {
				alertTshow(0);
				return;
			}
			if(!goodsBuyNum) {
				alertTshow(1);
				return;
			}
			//			if(!goodsPrice){
			//				alertTshow(2);
			//				return;
			//			}

			var goodsId = $("#goodsName").attr("data-id");
			var goodsBuyNum = $("#goodsNum").val(); //购买数量
			var goodsPrice = $("#goodsPrice").val(); //产品单价
			var allMoney = goodsBuyNum * goodsPrice; //产品数量*单价（无折扣）

			var notes = $("#notes").val(); //备注
			var goodsStock = $("#goodsStock").text();

			var goodsInfo = {
				"goodsSerial": $("#goodsSerial").text(),
				"goodsId": $("#goodsName").attr("data-id"),
				"goodsName": $("#goodsName").text(),
				"goodsBuyNum": goodsBuyNum,
				"goodsPrice": goodsPrice,
				"goodsStock": goodsStock,
				"goodsWhid": goodsWhid,
				"goodsWhName": goodsWhName,
				"goodsUnit": $("#goodsName").attr("data-unit"),
				"notes": $("#notes").val()
			};
			sessionStorage.removeItem("goodsSerial");
			sessionStorage.removeItem("goodsName");
			sessionStorage.removeItem("goodsStock");
			sessionStorage.removeItem("goodsId");
			sessionStorage.removeItem("libraryName");
			sessionStorage.removeItem("libraryNameId");
			console.log(goodsInfoList);
			for(var i = 0; i < goodsInfoList.length; i++) {
				if(goodsId == goodsInfoList[i].goodsId) {
					goodsInfoList.splice(i, 1);
					break;
				}
				goodsInfoList[i] = JSON.stringify(goodsInfoList[i]);
			}
			goodsInfoList.push(goodsInfo);

			for(var i = 0; i < goodsInfoList.length; i++) {
				if(typeof goodsInfoList[i] == "object") {
					goodsInfoList[i] = JSON.stringify(goodsInfoList[i]);
				}
				console.log(typeof goodsInfoList[i]);

			}
			sessionStorage.goodsInfoList = JSON.stringify(goodsInfoList); //产品信息本地储存
			//			console.log(sessionStorage.goodsInfoList);
			mui.back();
		}

		count();

		function count() {
			var goodsBuyNum = $("#goodsNum").val(); //购买数量
			var goodsPrice = $("#goodsPrice").val(); //产品单价

			$("#fromInstockMoney").text((goodsBuyNum * goodsPrice).toFixed(2));
		}

		$("body").on("keyup", "#goodsNum", function() {
			count(sessionStorage.setParm);
		});
		$("body").on("keyup", "#goodsPrice", function() {
			count(sessionStorage.setParm);
		});

	});

});